package com.thinvent.recommend.web.controller;

import com.thinvent.recommend.common.vo.ResponseResult;
import com.thinvent.recommend.manager.dto.RecommendedDocDTO;
import com.thinvent.recommend.service.RecommendationService;
import com.thinvent.recommend.service.Text2VecService;
import com.thinvent.recommend.service.UserDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 推荐接口 Controller
 */
@RestController
@RequestMapping("/recommend")
public class RecommendationController extends BaseController{

    private static final Logger log = LoggerFactory.getLogger(RecommendationController.class);

    @Autowired
    private RecommendationService recommendationService;

    @Autowired
    private UserDataService userDataService;

    @Autowired
    private Text2VecService text2VecService;

    /**
     * GET /api/recommend/user?userId={userId}
     * 根据用户的行为数据的推荐rest接口，参数为用户的id
     */
    @GetMapping("/user")
    public ResponseResult recommendForUser(
            @RequestParam("userId") String userId) {
        log.info("收到推荐请求，userId={}", userId);
        List<RecommendedDocDTO> recs = recommendationService.recommendByUser(userId);
        if (recs.isEmpty()) {
            return success();
        }
        return success(recs);
    }

    /**
     * GET /api/recommend/position?userId={userId}
     * 根据用户的岗位关键词推荐文档
     */
    @GetMapping("/position")
    public ResponseResult recommendByPosition(
            @RequestParam("userId") String userId) {
        log.info("收到岗位推荐请求，userId={}", userId);
        List<RecommendedDocDTO> recs = recommendationService.recommendByPosition(userId);
        if (recs.isEmpty()) {
            return success();
        }
        return success(recs);
    }

    /**
     * GET /api/recommend/history
     * 根据用户所在部门的历史操作记录推荐文档
     */
    @GetMapping("/history")
    public ResponseResult recommendByDeptHistory(
            @RequestParam("userId") String userId) {
        log.info("收到部门历史推荐请求，userId={}", userId);
        List<RecommendedDocDTO> recs = recommendationService.recommendByDeptHistory(userId);
        if (recs.isEmpty()) {
            return success();
        }
        return success(recs);
    }

    /**
     * 根据用户id查询用户的行为数据（预览、下载、收藏、编辑、点赞这五种）
     * @param userId
     * @return
     */
    @GetMapping("/userData")
    public ResponseResult getUserData(@RequestParam("userId") String userId) {
        return success(userDataService.getUserData(userId));
    }

    /**
     * 全量或增量同步知识库到 ES 向量库
     *
     * @param startTime 可选：只同步 updateTime >= startTime
     * @param endTime   可选：只同步 updateTime <= endTime
     */
    @GetMapping("/processFileInfoPages")
    public ResponseResult processFileInfoPages(
            @RequestParam(value = "startTime", required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
                    LocalDateTime startTime,

            @RequestParam(value = "endTime", required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
                    LocalDateTime endTime
    ) {
        log.info("触发向量化同步，startTime={}，endTime={}", startTime, endTime);
        text2VecService.processFileInfoPages(startTime, endTime);
        return success();
    }
}
